package com.tiecode.pastejava;

import com.sun.source.tree.VariableTree;
import com.sun.tools.javac.tree.JCTree;
import com.sun.tools.javac.tree.Pretty;
import com.sun.tools.javac.util.Name;

import java.io.Writer;
import java.util.List;

public class MyPretty extends Pretty {
    private List<VariableTree> classVars;
    private List<? extends VariableTree> methodParams;

    public MyPretty(Writer out, boolean sourceOutput, List<VariableTree> classVars, List<? extends VariableTree> methodParams) {
        super(out, sourceOutput);
        this.classVars = classVars;
        this.methodParams = methodParams;
    }

    @Override
    public void visitIdent(JCTree.JCIdent tree) {
        try {
            if (isVarRef(tree.name)) {
                print("#" + tree.name);
            } else {
                print(tree.name);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private boolean isVarRef(Name name) {
        for (VariableTree classVar : classVars) {
            if (classVar.getName() == name) {
                return true;
            }
        }
        if (methodParams != null) {
            for (VariableTree param : methodParams) {
                if (param.getName() == name) {
                    return true;
                }
            }
        }
        return false;
    }
}
